package vg.modules.backlight;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.*;

import vg.services.data_base_manager.data.graph.Graph;
import vg.services.data_base_manager.data.graph.Vertex;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.graph_view.IGraphRenderer;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.actions.DefaultAction;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.actions.DefaultActionAdapter;
import vg.services.main_manager.MainManager;

/**
 * This plugin finds cycle in current subgraph.
 *
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
*/
public class CycleSearcher {
	private JButton button;
	private AGraphView view;

	public CycleSearcher() {
		button = new JButton(new ImageIcon("./data/resources/textures/circle.png"));
		button.setToolTipText("Find all cycles, which have current vertex");
        button.setEnabled(false);
        button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (view != null) {
                    view.addAction(new DefaultActionAdapter("cycle searching action") {
                        @Override
                        public void action(final IGraphRenderer graphRenderer) {
                            Graph graph = graphRenderer.getOriginalSelectedSubGraph();
                            if (graph != null) {
                                Collection<Vertex> lv = graph.getVertices();
                                if (lv != null && lv.size() == 1) {
                                    Vertex sv = lv.iterator().next();
                                    graphRenderer.showCycles(sv);
                                }
                            }
                        }
                    });
                }
			}
		});
	}

	public JComponent getView() {
		return button;
	}
	
	public void setEnabled(boolean enable) {
		button.setEnabled(enable);
	}
	public void changeView(AGraphView igv) {		
		view = igv;
		if (view == null) {
			setEnabled(false);
		}
	}
}